/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.base.bean;

import com.ibatis.dao.client.DaoManager;
import com.yunhoo.base.db.dao.AssetGBCodeDao;
import com.yunhoo.base.model.AssetGBCode;
import com.yunhoo.sys.db.config.DaoConfig;
import com.yunhoo.sys.util.tree.MyTreeNodeImpl;
import com.yunhoo.sys.util.tree.TreeModel;
import com.yunhoo.sys.util.tree.TreeRule;
import com.yunhoo.sys.util.tree.TreeUtil;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.richfaces.component.UITree;
import org.richfaces.event.TreeToggleEvent;
import org.richfaces.model.TreeNode;
import org.richfaces.model.TreeNodeImpl;

@ManagedBean(name = "assetGBCodeLazyLoadBean")
@SessionScoped
public class AssetGBCodeLazyLoadBean implements Serializable {

    private static final DaoManager daom = DaoConfig.getDaoManager();
    private static final AssetGBCodeDao dao = (AssetGBCodeDao) daom.getDao(AssetGBCodeDao.class);
    private static final String[] rule = TreeRule.getAssetGBCodeTreeRule();
    //
    private AssetGBCode node = null;
    private TreeNode rootNode = null;
    private Set<AssetGBCode> gbCodeSet = null;

    private void addNode(List ls, TreeNode rootNode) {

        for (int i = 0; i < ls.size(); i++) {
            TreeModel item = (TreeModel) ls.get(i);

            Boolean isleaf = 1 == item.getIsEnd(); //true : 是叶子节点

            MyTreeNodeImpl currentNode = new MyTreeNodeImpl(isleaf, item);

            rootNode.addChild(item.getCode().trim(), currentNode);
        }
        
        Iterator it = rootNode.getChildrenKeysIterator();
        while (it.hasNext()) {
            System.out.println("next : " + it.next());
        }
    }

    public void toggleListener(TreeToggleEvent event) {
        System.out.println("collapsed : " + event.isCollapsed());
        System.out.println("expended : " + event.isExpanded());
        if (event.isExpanded()) {//展开节点

            UITree tree = (UITree) event.getSource();
            MyTreeNodeImpl currentNode = (MyTreeNodeImpl) tree.getRowData();
            TreeModel row = (TreeModel) currentNode.getData();

            System.out.println("row code : " + row.getCode());
            System.out.println("row name : " + row.getName());
            System.out.println("row supCode : " + row.getSupCode());

            AssetGBCode param = new AssetGBCode();
            param.setGrade(row.getGrade() + 1);
            param.setCode(row.getCode());

            List<AssetGBCode> list = dao.queryList(param);

            if (list == null) {
                return;
            }

            System.out.println("list size : " + list.size());
            addNode(list, currentNode);
        }
    }

    private void loadTree() {
        System.out.println("load cateTree");
        rootNode = new TreeNodeImpl();

        AssetGBCode param = new AssetGBCode();
        param.setGrade(1);
        List<AssetGBCode> list = dao.queryList(param);
        
        System.out.println("rootNode list size : " + list.size());
        
        TreeUtil.addNode(list, rootNode, rule);
    }
    
    public TreeNode getRootNode() {
        if (this.rootNode == null) {
            loadTree();
        }
        return rootNode;
    }
}
